package com.ly.algorithm.DataStructures.stack;

/**
 * @Classname ArrayStack
 * @Description TODO
 * @Date 2020/4/22 15:47
 * @Author 冷心影翼
 */
public class ArrayStack {

    private int maxSize;

    private int[] stack;

    private int top = -1;

    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[maxSize];
    }

    public boolean isFull() {
        if( top == maxSize -1) {
            return true;
        } else {
            return false;
        }
    }

    public boolean isEmpty() {
        if(top == -1) {
            return true;
        } else {
            return false;
        }
    }

    public void push(int value) {
        if(isFull()) {
            throw new RuntimeException("栈满");
        }
        top ++;
        stack[top] = value;

    }

    public int pop() {
        if(isEmpty()) {
            throw new RuntimeException("栈空");
        }
        int value = stack[top];
        top--;
        return value;
    }

    public void list() {
        for(int i=top;i>=0;i--) {
            System.out.println(stack[i]);
        }
    }
}

class Test {
    public static void main(String[] args) {
        ArrayStack arrayStack = new ArrayStack(4);
        arrayStack.push(1);
        arrayStack.push(2);
        arrayStack.push(3);
        arrayStack.push(4);
        arrayStack.list();
        System.out.println("---------");
        arrayStack.pop();
        arrayStack.pop();
        arrayStack.list();
    }
}